package com.hk.core.autoconfigure.data;

import com.hk.commons.multischema.DataSourceType;
import com.hk.commons.util.StringUtils;
import com.hk.core.authentication.api.UserPrincipal;

import java.util.Objects;
import java.util.function.Function;

/**
 * @author Kevin
 * @date 2024-04-21 12:48
 */
public final class PrincipalTenantResolverProvider implements TenantIdResolverProvider {

    @Override
    public boolean test(String value) {
        return StringUtils.equals(DataSourceType.PRINCIPAL_TENANT_ID, value);
    }

    @Override
    public Function<UserPrincipal, String> getProvider() {
        return principal -> Objects.isNull(principal.getTenantInfo()) ||
                Objects.isNull(principal.getTenantInfo().getTenantId()) ?
                null : String.valueOf(principal.getTenantInfo().getTenantId());
    }
}
